import javax.swing.text.AttributeSet;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: user
 * Date: 2023-06-14
 * Time: 10:43
 */
public class Solution {
    public boolean isvalid (String s){
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i <s.length() ; i++) {
            char ch = s.charAt(i);
            if(ch=='(' ||ch=='[' || ch=='{'){
                stack.push(ch);
            }else {
                if(stack.empty()){
                    return false;
                }else {
                    int tmp = stack.peek();
                    if(ch==')' && tmp =='(' || ch=='}' &&tmp=='{' || ch==']' &&ch=='['){
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }
}
